package softWareConstructPro;//coding with utf-8

import java.util.ArrayList;

public class Exercise {
	private int maxkey, capacity, op_status;//maxkey，最大限定值，最小限定值默认0; capacity，算式数量; op_status，总体算式类型，0:加减混合 1：全加法，-1：全减法
	private boolean using_status;//using_status:是否正在使用，为true则拒绝修改函数的修改
	private ArrayList<BinaryOperation> list = new ArrayList<>();//动态BinaryOperation数组，方便修改题量
	public Exercise() {//默认构造函数
		this.maxkey = 100;
		this.capacity = 50;
		this.op_status = 0;
		this.using_status = false;
		//construct begin
		
	}
	public Exercise(int mk, int cap, int op) {//自定构造函数
		this.maxkey = mk;
		this.capacity = cap;
		this.op_status = op;
		this.using_status = false;
		//construct begin
		
	}
	public int getMaxKey() {//得到maxkey
		return this.maxkey;
	}
	public boolean setMaxKey(int neoKey) {//修改maxeky
		if(this.using_status == true)
			return false;//program using, refuse to change
		else {
			this.maxkey = neoKey;
			return true;
		}
	}
	public int getCapacity() {//得到算式数量
		return this.capacity;
	}
	public boolean setCapacity(int neoCapacity) {//改变算式数量
		if(this.using_status == true) {
			return false;//program using, refuse to change
		}
		else {
			this.capacity = neoCapacity;
			return true;
		}
	}
	public int getOp() {//得到算式类型
		return this.op_status;
	}
	public boolean setOp(int op) {//改变算式类型
		if(this.using_status == true) {
			return false;
		}
		else {
			this.op_status = op;
			return true;
		}
	}
	public void setUsing() {//开启做题模式
		this.using_status = true;
	}
	public boolean ifUsing() {//判断是否在做题
		return this.using_status;
	}
	public void afterUsed() {//在确认题目做完，程序保存相关数据之后，将题目清空，置做题状态为否
		this.using_status = false;
		list.clear();
	}
	private void generateAdditionExercise() {//利用生成练习题函数，只生成加法题
		generateExercise(1);
	}
	private void generateSubstractionExercise() {//利用练习题函数，之生成减法题
		generateExercise(-1);
	}
	private void generateExercise(int op) {//根据op的值来决定生成哪种题
		for(int i = 0; i < this.capacity; ++i) {
			BinaryOperation bt = new BinaryOperation();
			//System.out.println(i+" th exercise created");
			do {
				if(op == 0) {
					bt.generateBinaryOperation(this.maxkey,0);//调用BianryOperation的函数生成单个题目
				}
				else if(op == 1) {
					bt.generateAdditionOperation(this.maxkey);
				}
				else {
					bt.generateSubstractionOperation(this.maxkey);
				}
			}while(exerciseTester(bt) == false);//判断是否有重复的题目，来决定是否继续循环
			list.add(bt);//将生成的一个算式加入到动态数组中
		}
	}
	private boolean exerciseTester(BinaryOperation bt) {//判断是否有重复的算式
		boolean check = true;
		int len = list.size();
		for(int i = 0; i < len; ++i) {
			if(bt.equal(list.get(i)) == true) {//BinaryOperation的equal函数派上用场
				check = false;
				break;
			}
		}
		return check;
	}
	public void regenerateExercise() {//重新生成试题
		this.afterUsed();//首先清空试题
		//whether add using_status = true
		this.setUsing();
		if(this.op_status == 0) {
			generateExercise(0);
		}
		else if(this.op_status == 1) {
			generateAdditionExercise();
		}
		else {
			generateSubstractionExercise();
		}
	}
	public BinaryOperation getBO(int index) {//得到动态数组的具体位置的试题，当没有使用或者超过动态数组界限时返回空值
		if(using_status == false) {
			return null;
		}
		else {
			if(index < list.size())
				return list.get(index);
			else {
				return null;
			}
		}
	}
	public boolean insertBO(BinaryOperation bot) {//添加算式函数
		boolean check = true;
		if(this.using_status == true) {
			check = false;
		}
		else {
			this.list.add(bot);
		}
		return check;
	}
	public String toString() {
		int len = list.size();
		String ans = "";
		for(int i = 0; i < len; ++i) {
			ans+=list.get(i).toString();
		}
		return ans;
	}
	public void printExercise(boolean flag) {//将生成的试题全部在控制台打印出来
		int len = list.size();
		System.out.println("" + len + " printed:");
		for(int i = 0; i < len; ++i) {
			if(flag == true)
				System.out.print(String.format("%-3d", i+1) + ": " + list.get(i) +""+ String.format("%-5d", list.get(i).getResult())+ "      ");
			else
				System.out.print(String.format("%-3d", i+1) + ": " + list.get(i) + "      ");
			if((i+1)%5==0)
				System.out.println();
		}
	}
	/*
	public static void main(String[] argv) {
		Exercise myex = new Exercise();//use for test
		myex.regenerateExercise();
		myex.printExercise(true);
		//System.out.println(myex);
	}*/
}
